package com.testtask.rightandabove.dao;

import com.testtask.rightandabove.entity.Catalog;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.springframework.stereotype.Component;

/**
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
@Component
public class CDWarehouseDaoImpl implements ICDWarehouseDao {

    @Override
    public Catalog readAll(File file) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Catalog.class);
        Unmarshaller u = context.createUnmarshaller();
        Catalog catalog = (Catalog) u.unmarshal(file);
        return catalog;
    }

    @Override
    public void updateAll(Catalog catalog, File file) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Catalog.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(catalog, file);
    }
}
